package com.atguigu.gmall.product.service.impl;
import com.atguigu.gmall.product.entity.*;
import com.atguigu.gmall.product.service.*;
import com.atguigu.gmall.product.vo.CategoryTreeVo;
import com.atguigu.gmall.search.SearchAttr;
import com.google.common.collect.Lists;
import java.util.Date;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.feign.search.SearchFeignClient;
import com.atguigu.gmall.product.vo.SkuSaveVo;
import com.atguigu.gmall.search.Goods;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.mapper.SkuInfoMapper;
import groovy.util.logging.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Slf4j
@Service
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo>
    implements SkuInfoService{

    @Autowired
    SkuAttrValueService skuAttrValueService;

    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    SkuImageService skuImageService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    SearchFeignClient searchFeignClient;

    @Autowired
    BaseTrademarkService baseTrademarkService;

    @Autowired
    BaseCategory1Service baseCategory1Service;

    @Transactional
    @Override
    public void saveSkuInfo(SkuSaveVo skuSaveVo) {
        //1.存入skuinfo
        SkuInfo skuInfo = new SkuInfo();
        BeanUtils.copyProperties(skuSaveVo, skuInfo);
        save(skuInfo);
        //获取skuid
        Long skuId = skuInfo.getId();
        //2.存入sku attr value  平台属性
        List<SkuAttrValue> skuAttrValueList = skuSaveVo.getSkuAttrValueList().stream()
                .map(item -> {
                    SkuAttrValue skuAttrValue = new SkuAttrValue();
                    skuAttrValue.setAttrId(item.getAttrId());
                    skuAttrValue.setValueId(item.getValueId());
                    skuAttrValue.setSkuId(skuId);
                    return skuAttrValue;
                }).collect(Collectors.toList());
        skuAttrValueService.saveBatch(skuAttrValueList);
        //3.存入sku sale attr value  销售属性
        List<SkuSaleAttrValue> skuSaleAttrValueList = skuSaveVo.getSkuSaleAttrValueList().stream()
                .map(item -> {
                    SkuSaleAttrValue skuSaleAttrValue = new SkuSaleAttrValue();
                    skuSaleAttrValue.setSkuId(skuId);
                    skuSaleAttrValue.setSpuId(skuInfo.getSpuId());
                    skuSaleAttrValue.setSaleAttrValueId(item.getSaleAttrValueId());
                    return skuSaleAttrValue;
                }).collect(Collectors.toList());
        skuSaleAttrValueService.saveBatch(skuSaleAttrValueList);
        //4.存入sku image  商品图片
        List<SkuImage> skuImageList = skuSaveVo.getSkuImageList().stream()
                .map(item -> {
                    SkuImage skuImage = new SkuImage();
                    skuImage.setSkuId(skuId);
                    skuImage.setImgName(item.getImgName());
                    skuImage.setImgUrl(item.getImgUrl());
                    skuImage.setSpuImgId(item.getSpuImgId());
                    skuImage.setIsDefault(item.getIsDefault());
                    return skuImage;
                }).collect(Collectors.toList());
        skuImageService.saveBatch(skuImageList);

        //同步位面
        stringRedisTemplate.opsForValue().setBit(RedisConst.SKUID_BITMAP, skuId, true);
    }

    //上架
    @Override
    public void upGoods(Long skuId) {
        //修改数据库上下架状态
        boolean update = lambdaUpdate().set(SkuInfo::getIsSale, 1).eq(SkuInfo::getId, skuId).update();
        //保存至es中
        if (update){
            Goods goods = prepareGoods(skuId);
            searchFeignClient.up(goods);
            log.info("商品【{}】上架成功",skuId);
        }
    }

    private Goods prepareGoods(Long skuId) {

        SkuInfo skuInfo = getById(skuId);
        Goods goods = new Goods();

        goods.setId(skuInfo.getId());
        goods.setDefaultImg(skuInfo.getSkuDefaultImg());
        goods.setTitle(skuInfo.getSkuName());
        goods.setPrice(skuInfo.getPrice().doubleValue());
        goods.setCreateTime(new Date());

        //查询品牌
        BaseTrademark trademark = baseTrademarkService.getById(skuInfo.getTmId());
        goods.setTmId(trademark.getId());
        goods.setTmName(trademark.getTmName());
        goods.setTmLogoUrl(trademark.getLogoUrl());

        //三级分类
        CategoryTreeVo categoryTree = baseCategory1Service.getCategoryTreeByCId(skuInfo.getCategory3Id());
        goods.setCategory1Id(categoryTree.getCategoryId());
        goods.setCategory1Name(categoryTree.getCategoryName());

        CategoryTreeVo categoryTreeVo = categoryTree.getCategoryChild().get(0);
        goods.setCategory2Id(categoryTreeVo.getCategoryId());
        goods.setCategory2Name(categoryTreeVo.getCategoryName());

        CategoryTreeVo categoryTreeVo1 = categoryTreeVo.getCategoryChild().get(0);
        goods.setCategory3Id(categoryTreeVo1.getCategoryId());
        goods.setCategory3Name(categoryTreeVo1.getCategoryName());

        //热度
        goods.setHotScore(0L);

        //平台属性
        List<SearchAttr> attrs = skuAttrValueService.getSkuAttrsAndValue(skuId);
        goods.setAttrs(attrs);

        return goods;
    }

    @Override
    public void downGoods(Long skuId) {
        boolean update = lambdaUpdate().set(SkuInfo::getIsSale, 0).eq(SkuInfo::getId, skuId).update();
        if (update){
            searchFeignClient.down(skuId);
            //缓存，bitmap同步
            log.info("商品【{}】下架成功",skuId);
        }
    }
}




